package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.Setmeal_DishesMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private Setmeal_DishesMapper setmeal_dIshesMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        setmealDTO.getSetmealDishes().forEach(item->{
            item.setSetmealId(setmealMapper.getByName(setmeal.getName()).longValue());
        });

        setmeal_dIshesMapper.save(setmealDTO.getSetmealDishes());
    }



    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //启动分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> list = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    public void deleteById(Long[] ids) {
        List<Long> idslist = Arrays.stream(ids).toList();
        setmealMapper.deleteById(idslist);
        setmeal_dIshesMapper.deleteByDishId(idslist);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getById(id);
        return setmealVO;

    }

    @Override
    public void update(SetmealDTO setmealDTO) {

        setmealMapper.update(setmealDTO);
        setmeal_dIshesMapper.deleteByDishId(Arrays.asList(setmealDTO.getId()));

        for(SetmealDish setmealDish : setmealDTO.getSetmealDishes()){
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmeal_dIshesMapper.save(setmealDTO.getSetmealDishes());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        SetmealDTO setmealDTO = new SetmealDTO();
        setmealDTO.setStatus(status);
        setmealDTO.setId(id);
        setmealMapper.update(setmealDTO);
    }

    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}

